﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace DailyNote.AppCode
{
    public interface IDailyNote
    {
        //日报
        string DayDaily(DateTime dateTime);
        //周报
        string WeeklyDaily(DateTime dateTime);
        //月报
        string MonthlyDaily(DateTime dateTime);
        //年报 
        string YearDaily(DateTime dateTime);
    }
    public class GDailyNote : IDailyNote
    {
        public string DayDaily(DateTime dateTime)
        {
            string context = "";
            string str = string.Concat(new object[] { "select note from note where year=", dateTime.Year, " and month=", dateTime.Month, " and day=", dateTime.Day });
            SQLiteDataReader reader = SQLiteDBHelper.ExecuteReader(str);
            if (reader.Read())
            {
                context = reader["note"].ToString();
            }
            return context;
        }
        public string WeeklyDaily(DateTime dateTime)
        {
            //本周(要知道本周的第一天就得先知道今天是星期几，从而得知本周的第一天就是几天前的那一天，要注意的是这里的每一周是从周日始至周六止     
            DateTime ben = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(dateTime.DayOfWeek))));
            DateTime end = DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(dateTime.DayOfWeek))));
            DateTime now = DateTime.Now;
            string title = Utils.Author + "_周报_" + ben.ToLongDateString() + "---" + end.ToLongDateString();
            string str = string.Concat(new object[] { "select * from note where year=", now.Year, " and month=", now.Month, " and day>=", ben.Day, " and day<=", end.Day });
            DataSet ds = SQLiteDBHelper.ExecuteDataset(str);

            StringBuilder list = new StringBuilder();
            list.AppendLine(title);
            list.AppendLine("");
            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string note = dt.Rows[i]["note"].ToString();
                    string year = dt.Rows[i]["year"].ToString();
                    string month = dt.Rows[i]["month"].ToString();
                    string day = dt.Rows[i]["day"].ToString();
                    list.AppendLine(day + "：" + note);
                    list.AppendLine("=============================================================================");
                }
            }
            return list.ToString();
        }
        public string MonthlyDaily(DateTime dateTime)
        {
            string str = string.Concat(new object[] { "select * from note where year=", dateTime.Year, " and month=", dateTime.Month });
            DataSet ds = SQLiteDBHelper.ExecuteDataset(str);
            string title = Utils.Author + "_月报_" + dateTime.ToString("yyyy/MM");
            StringBuilder list = new StringBuilder();
            list.AppendLine(title);
            list.AppendLine("");
            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string note = dt.Rows[i]["note"].ToString();
                    string year = dt.Rows[i]["year"].ToString();
                    string month = dt.Rows[i]["month"].ToString();
                    string day = dt.Rows[i]["day"].ToString();
                    list.AppendLine(month + "-" + day + "：" + note);
                    list.AppendLine("=============================================================================");
                }
            }
            return list.ToString();
        }
        public string YearDaily(DateTime dateTime)
        {
            string str = string.Concat(new object[] { "select * from note where year=", dateTime.Year });
            DataSet ds = SQLiteDBHelper.ExecuteDataset(str);
            string title = Utils.Author + "_年报_" + dateTime.Year.ToString();
            StringBuilder list = new StringBuilder();
            list.AppendLine(title);
            list.AppendLine("");
            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string note = dt.Rows[i]["note"].ToString();

                    string year = dt.Rows[i]["year"].ToString();
                    string month = dt.Rows[i]["month"].ToString();
                    string day = dt.Rows[i]["day"].ToString();

                    list.AppendLine(year + "-" + month + "：" + note);
                    list.AppendLine("=============================================================================");
                }
            }
            return list.ToString();

        }
    }
}
